{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import sys\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_325 = pd.read_excel(\"3-数据清洗后325样本数据.xlsx\", na_values=np.nan)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['样本编号', '时间', 'RON损失', '原料性质：硫含量', '原料性质：辛烷值', '原料性质：饱和烃', '原料性质：烯烃',\n",
       "       '原料性质：芳烃', '原料性质：溴值', '原料性质：密度',\n",
       "       ...\n",
       "       'S-ZORB.AT-0011.DACA.PV', 'S-ZORB.FT_1204.DACA.PV',\n",
       "       'S-ZORB.LC_5102.PIDA.PV', 'S-ZORB.TE_1102.DACA.PV',\n",
       "       'S-ZORB.CAL.LINE.PV', 'S-ZORB.CAL.CANGLIANG.PV', 'S-ZORB.CAL.SPEED.PV',\n",
       "       'S-ZORB.FT_1503.TOTALIZERA.PV', 'S-ZORB.FT_1504.TOTALIZERA.PV',\n",
       "       'S-ZORB.PC_1001A.PV'],\n",
       "      dtype='object', length=203)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_325.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "head_data = data_325[data_325.columns[:16]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data_325 = data_325[data_325.columns[16:]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "pca = PCA(n_components=30) #实例化\n",
    "pca = pca.fit(test_data_325) #拟合模型\n",
    "new_data_325 = pca.transform(test_data_325) #获取降维后的新矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8.32939152e+14, 2.13850174e+14, 1.24433430e+12, 4.31659172e+11,\n",
       "       1.55696998e+11, 2.68850109e+10, 2.20498328e+09, 1.42527847e+09,\n",
       "       1.80014899e+08, 1.66950203e+08, 5.18783297e+07, 8.36026455e+06,\n",
       "       4.48775827e+06, 8.76966320e+05, 5.34908574e+05, 1.68286350e+05,\n",
       "       9.80449757e+04, 5.68477215e+04, 2.62496393e+04, 1.85307473e+04,\n",
       "       1.60675607e+04, 1.40347426e+04, 8.58354285e+03, 4.32969747e+03,\n",
       "       3.96310594e+03, 2.20723737e+03, 1.34819425e+03, 9.62525222e+02,\n",
       "       9.09637640e+02, 6.87808237e+02])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.explained_variance_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.34043488e+07, -2.77772120e+07,  2.56605318e+06, ...,\n",
       "        -5.40983169e+01, -4.32394982e+00,  1.29133903e+01],\n",
       "       [-2.34095091e+07, -2.74167661e+07,  2.53078192e+06, ...,\n",
       "        -2.36516796e+01,  7.93534579e+00,  4.06564856e+00],\n",
       "       [-2.34112235e+07, -2.72710756e+07,  2.50522708e+06, ...,\n",
       "        -3.35247336e+00, -9.22861499e+00,  1.64865401e-01],\n",
       "       ...,\n",
       "       [ 3.28899003e+07,  1.42208543e+07,  2.28407594e+06, ...,\n",
       "         4.14188759e+00, -2.58302746e+01, -1.99589052e+01],\n",
       "       [ 3.28665586e+07,  1.43649368e+07,  2.33286461e+06, ...,\n",
       "         4.58144422e+00, -1.29285063e+01, -1.40279164e+01],\n",
       "       [ 3.28417657e+07,  1.45089170e+07,  2.38142049e+06, ...,\n",
       "        -6.92661031e+00,  7.63810816e+00, -9.07022617e+00]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_data_325"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>...</th>\n",
       "      <th>20</th>\n",
       "      <th>21</th>\n",
       "      <th>22</th>\n",
       "      <th>23</th>\n",
       "      <th>24</th>\n",
       "      <th>25</th>\n",
       "      <th>26</th>\n",
       "      <th>27</th>\n",
       "      <th>28</th>\n",
       "      <th>29</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-2.340435e+07</td>\n",
       "      <td>-2.777721e+07</td>\n",
       "      <td>2.566053e+06</td>\n",
       "      <td>-1.837130e+06</td>\n",
       "      <td>340296.571054</td>\n",
       "      <td>-23074.520637</td>\n",
       "      <td>-2422.639270</td>\n",
       "      <td>33968.154023</td>\n",
       "      <td>4878.848915</td>\n",
       "      <td>4732.719113</td>\n",
       "      <td>...</td>\n",
       "      <td>186.500005</td>\n",
       "      <td>-78.463090</td>\n",
       "      <td>-1.529687</td>\n",
       "      <td>61.720509</td>\n",
       "      <td>29.765721</td>\n",
       "      <td>-55.943617</td>\n",
       "      <td>49.491772</td>\n",
       "      <td>-54.098317</td>\n",
       "      <td>-4.323950</td>\n",
       "      <td>12.913390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-2.340951e+07</td>\n",
       "      <td>-2.741677e+07</td>\n",
       "      <td>2.530782e+06</td>\n",
       "      <td>-1.808784e+06</td>\n",
       "      <td>342680.341485</td>\n",
       "      <td>-17384.190746</td>\n",
       "      <td>-6204.717351</td>\n",
       "      <td>36472.142420</td>\n",
       "      <td>6483.238588</td>\n",
       "      <td>4010.160632</td>\n",
       "      <td>...</td>\n",
       "      <td>46.482887</td>\n",
       "      <td>-114.976753</td>\n",
       "      <td>-24.094824</td>\n",
       "      <td>69.751353</td>\n",
       "      <td>-5.854583</td>\n",
       "      <td>-33.796075</td>\n",
       "      <td>11.617771</td>\n",
       "      <td>-23.651680</td>\n",
       "      <td>7.935346</td>\n",
       "      <td>4.065649</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.341122e+07</td>\n",
       "      <td>-2.727108e+07</td>\n",
       "      <td>2.505227e+06</td>\n",
       "      <td>-1.784640e+06</td>\n",
       "      <td>334590.653268</td>\n",
       "      <td>-14373.719870</td>\n",
       "      <td>-8064.698134</td>\n",
       "      <td>36324.561815</td>\n",
       "      <td>8083.330462</td>\n",
       "      <td>3645.060678</td>\n",
       "      <td>...</td>\n",
       "      <td>17.334831</td>\n",
       "      <td>-141.875672</td>\n",
       "      <td>53.736227</td>\n",
       "      <td>62.692694</td>\n",
       "      <td>26.287395</td>\n",
       "      <td>-44.978162</td>\n",
       "      <td>6.153710</td>\n",
       "      <td>-3.352473</td>\n",
       "      <td>-9.228615</td>\n",
       "      <td>0.164865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.341863e+07</td>\n",
       "      <td>-2.691241e+07</td>\n",
       "      <td>2.445042e+06</td>\n",
       "      <td>-1.730789e+06</td>\n",
       "      <td>314799.353277</td>\n",
       "      <td>-6876.216855</td>\n",
       "      <td>-14081.439345</td>\n",
       "      <td>36992.223874</td>\n",
       "      <td>12766.011080</td>\n",
       "      <td>2376.689261</td>\n",
       "      <td>...</td>\n",
       "      <td>90.571920</td>\n",
       "      <td>-91.176613</td>\n",
       "      <td>-42.904446</td>\n",
       "      <td>-1.875924</td>\n",
       "      <td>22.239335</td>\n",
       "      <td>-32.601358</td>\n",
       "      <td>33.363611</td>\n",
       "      <td>9.014862</td>\n",
       "      <td>-16.613198</td>\n",
       "      <td>-1.371619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-2.341998e+07</td>\n",
       "      <td>-2.676099e+07</td>\n",
       "      <td>2.413886e+06</td>\n",
       "      <td>-1.699464e+06</td>\n",
       "      <td>303063.333652</td>\n",
       "      <td>-5810.335621</td>\n",
       "      <td>-15180.177485</td>\n",
       "      <td>35449.120476</td>\n",
       "      <td>14999.853713</td>\n",
       "      <td>2234.055147</td>\n",
       "      <td>...</td>\n",
       "      <td>7.735753</td>\n",
       "      <td>12.053831</td>\n",
       "      <td>77.785005</td>\n",
       "      <td>3.934543</td>\n",
       "      <td>10.308469</td>\n",
       "      <td>-53.857578</td>\n",
       "      <td>6.525490</td>\n",
       "      <td>28.208454</td>\n",
       "      <td>-40.451397</td>\n",
       "      <td>1.595274</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 30 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             0             1             2             3              4   \\\n",
       "0 -2.340435e+07 -2.777721e+07  2.566053e+06 -1.837130e+06  340296.571054   \n",
       "1 -2.340951e+07 -2.741677e+07  2.530782e+06 -1.808784e+06  342680.341485   \n",
       "2 -2.341122e+07 -2.727108e+07  2.505227e+06 -1.784640e+06  334590.653268   \n",
       "3 -2.341863e+07 -2.691241e+07  2.445042e+06 -1.730789e+06  314799.353277   \n",
       "4 -2.341998e+07 -2.676099e+07  2.413886e+06 -1.699464e+06  303063.333652   \n",
       "\n",
       "             5             6             7             8            9   ...  \\\n",
       "0 -23074.520637  -2422.639270  33968.154023   4878.848915  4732.719113  ...   \n",
       "1 -17384.190746  -6204.717351  36472.142420   6483.238588  4010.160632  ...   \n",
       "2 -14373.719870  -8064.698134  36324.561815   8083.330462  3645.060678  ...   \n",
       "3  -6876.216855 -14081.439345  36992.223874  12766.011080  2376.689261  ...   \n",
       "4  -5810.335621 -15180.177485  35449.120476  14999.853713  2234.055147  ...   \n",
       "\n",
       "           20          21         22         23         24         25  \\\n",
       "0  186.500005  -78.463090  -1.529687  61.720509  29.765721 -55.943617   \n",
       "1   46.482887 -114.976753 -24.094824  69.751353  -5.854583 -33.796075   \n",
       "2   17.334831 -141.875672  53.736227  62.692694  26.287395 -44.978162   \n",
       "3   90.571920  -91.176613 -42.904446  -1.875924  22.239335 -32.601358   \n",
       "4    7.735753   12.053831  77.785005   3.934543  10.308469 -53.857578   \n",
       "\n",
       "          26         27         28         29  \n",
       "0  49.491772 -54.098317  -4.323950  12.913390  \n",
       "1  11.617771 -23.651680   7.935346   4.065649  \n",
       "2   6.153710  -3.352473  -9.228615   0.164865  \n",
       "3  33.363611   9.014862 -16.613198  -1.371619  \n",
       "4   6.525490  28.208454 -40.451397   1.595274  \n",
       "\n",
       "[5 rows x 30 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_data_325.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
